<?php
/**
 *@file auth.php
 *@author liulei
 *@date 2015-09-23
 */
namespace haibao\comment\common;

use haibao\frame\http\Cookie;
use haibao\classlibrary\Config;

class Auth{
	const SSO_USER_PUBLIC_USER = 0;
	const SSO_USER_PUBLIC_UNREGUSER = 1;
	
    public static function getUserInfo(){
        $userInfo = self::getUserByCookie();
        if(!$userInfo){
            $userInfo = self::getVisitorInfoByCookie();
        }
        return $userInfo;
    }
    /**
     * 根据cookie查询用户
     */
    public static function getUserByCookie(){
    	$cookie = Config::getConfig(Config::CLASSLIBRARY_CONFIG_COOKIE);
        $sessionId = \haibao\frame\http\Cookie::get($cookie['cookie_key']);
        if(!$sessionId){
            return false;
        }
    
        $cache = new \haibao\user\cache\User();
        $sessionData = $cache->getSession($sessionId);
        if(!$sessionData){
            return false;
        }
        $tempStr = base64_decode($sessionData);
        preg_match("/_auth_user_id = L([0-9]+)L/sU", $tempStr, $result);
        $userId = !empty($result[1]) ? $result[1] : '';
    
        preg_match("/_auth_nick_name = L(.*)L/sU", $tempStr, $result);
        $nickname = !empty($result[1]) ? $result[1] : '';
    
        preg_match("/_auth_user_name = L(.*)L/sU", $tempStr, $result);
        $username = !empty($result[1]) ? $result[1] : '';
         
        preg_match("/_auth_user_avator = L(.*)L/sU", $tempStr, $result);
        $avator = !empty($result[1]) ? $result[1] : '';
         
        return array(
            '_auth_user_id' => $userId,
            '_auth_nick_name' => $nickname,
            '_auth_user_name' => $username,
            '_auth_user_avator' => $avator,
            'sso_user_public'=> 0
        );
    }
    public static function getVisitorInfoByCookie(){
        $unRegUserId = self::getUnregUserId();
        $str = abs(crc32($unRegUserId));
        $string = str_pad($str, 4, "0");
        $rand = substr($string, 0,4);
        $name = '海报用户'.$rand;
        if($unRegUserId){
            return array(
                '_auth_user_id'=>$unRegUserId,
                '_auth_user_name'=>$name,
                '_auth_nick_name'=>$name,
                'sso_user_public'=>1
            );
        }else{
            return false;
        }
    }
    public static function getUnRegUserId(){
    	if (!self::getCookie('hbUnregUserId')){
    		self::setCookie('hbUnregUserId',self::getUniqueId());
    	}
    	return self::getCookie('hbUnregUserId');
    }
    
    public static function getUniqueId($length = null,$radix = null){
    	$chars = array(
    			'0','1','2','3','4','5','6','7','8','9','A','B',
    			'C','D','E','F','G','H','I','J','K','L','M','N','O','P',
    			'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d',
    			'e','f','g','h','i','j','k','l','m','n','o','p','q','r',
    			's','t','u','v','w','x','y','z'
    	);
    	
    	$uuid = array();
    	$radix = null;
    	
    	if (!$radix){
    		$radix = count($chars);
    	}
    	if ($length){
    		for ($i = 0 ; $i < $length ; $i++){
    			$index = floor(mt_rand(0,99)/100*$radix);
    			$uuid[$i] = $chars[$index];
    		}
    	}
    	else{
    		$r = null;
    		$uuid[8] = $uuid[13] = $uuid[18] = $uuid[23] = '-';
    		$uuid[14] = '4';
    	
    		for ($i = 0 ; $i < 36 ; $i++){
    			if (empty($uuid[$i])){
    				$r = floor(mt_rand(0,99)/100*16);
    				$index = ($i == 19) ? ($r & 0x3) | 0x8 : $r;
    				$uuid[$i] = $chars[$index];
    			}
    		}
    	}
    	
    	ksort($uuid);
    	
    	return implode('',$uuid);
    }
   	
    public static function getCookie($name){
    	$data = null;
    	if (isset($_COOKIE[$name])){
    		$data = $_COOKIE[$name];
    	}
    	
    	return $data;
    }
    
    public static function setCookie($name,$value){
    	setcookie($name,$value,time()+31*24*3600*1000,'/','haibao.com');
    	$_COOKIE[$name] = $value;
    }
}